<?php


namespace app\controllers\h5\uni\address;

/**
 *
 * 收货地址智能解析
 *
 * 简单分词处理, 后期可以用es
 * 不过, 这样已经够用了
 *
 * Class AddressTool
 * @package app\controllers\admin\uni\express
 *
 */
class AddressTool
{
    public static function parse($address)
    {
        $file = __DIR__ . '/data/data.json';
        $data = file_get_contents($file);
        $data = json_decode($data, true);

        // var_dump($data);exit;
        // var_dump('address -> ' . $address);exit;

        // 这种方法是解决, 同名的区问题
        $has_province = false;
        $has_city = false;

        // 直辖市处理
        $address = str_replace('上海', '上海直辖市', $address);
        $address = str_replace('上海市', '上海市直辖市', $address);
        $address = str_replace('北京', '北京直辖市', $address);
        $address = str_replace('北京市', '北京市直辖市', $address);
        $address = str_replace('天津', '天津直辖市', $address);
        $address = str_replace('天津市', '天津市直辖市', $address);
        $address = str_replace('重庆', '重庆直辖市', $address);
        $address = str_replace('重庆市', '重庆市直辖市', $address);

        // 从省开始
        foreach ($data as $province) {
            // 从省开始找
            if ((strpos($address, $province['name']) !== false) || (strpos($address, $province['name_']) !== false)) {
                $has_province = true;
                return self::_province($province, $address);
            }
        }

        // var_dump($has_province);

        // 从市开始
        if (!$has_province) {
            foreach ($data as $province) {
                foreach ($province['children'] as $city) {
                    if ((strpos($address, $city['name']) !== false)
                        || (strpos($address, $city['name_']) !== false)
                        || (strpos($address, $city['name__']) !== false)) {
                        $has_city = true;
                        return self::_city($city, $province, $address);
                    }
                }
            }
        }

        // var_dump($has_city);exit;

        // 从区开始
        if (!$has_province && !$has_city) {
            foreach ($data as $province) {
                foreach ($province['children'] as $city) {
                    foreach ($city['children'] as $area) {
                        if ((strpos($address, $area['name']) !== false)
                            || (strpos($address, $area['name_']) !== false)
                            || (strpos($address, $area['name__']) !== false)
                            || (strpos($address, $area['name___']) !== false)) {
                            return self::_area($area, $city, $province, $address);
                        }
                    }
                }
            }
        }

    }

    private static function _province($province, $address)
    {
        $area = [
            'name' => 0,
            'code' => 0,
        ];

        $city = [
            'name' => 0,
            'code' => 0,
        ];

        foreach ($province['children'] as $c) {
            if ((strpos($address, $c['name']) !== false)
                || (strpos($address, $c['name_']) !== false)
                || (strpos($address, $c['name__']) !== false)) {
                // $city = [
                //     'name' => $c['name'],
                //     'code' => $c['code'],
                // ];
                $city = $c;
                foreach ($c['children'] as $a) {
                    if ((strpos($address, $a['name']) !== false)
                        || (strpos($address, $a['name_']) !== false)
                        || (strpos($address, $a['name__']) !== false)
                        || (strpos($address, $a['name___']) !== false)) {
                        // $area = [
                        //     'name' => $a['name'],
                        //     'code' => $a['code'],
                        // ];
                        $area = $a;
                        break;
                    }
                }
            }
        }

        return self::_formatData($area, $city, $province, $address);

    }

    private static function _city($city, $province, $address)
    {
        $area = [
            'name' => 0,
            'code' => 0,
        ];

        foreach ($city['children'] as $a) {
            if ((strpos($address, $a['name']) !== false)
                || (strpos($address, $a['name_']) !== false)
                || (strpos($address, $a['name__']) !== false)
                || (strpos($address, $a['name___']) !== false)) {
                // $area = [
                //     'name' => $a['name'],
                //     'code' => $a['code'],
                // ];
                $area = $a;
                break;
            }
        }

        return self::_formatData($area, $city, $province, $address);
    }

    private static function _area($area, $city, $province, $address)
    {
        return self::_formatData($area, $city, $province, $address);
    }

    // 返回数据格式
    public static function _formatData($area, $city, $province, $address)
    {
        $info = [
            'province' => [
                'name' => $province['name'],
                'code' => $province['code'],
            ],
            'city' => [
                'name' => $city['name'],
                'code' => $city['code'],
            ],
            'area' => [
                'name' => $area['name'],
                'code' => $area['code'],
            ]
        ];

        // var_dump($area);

        if ($area['code'] > 0) {
            $a = '';
            if (isset($area['name___']) && ($pos = mb_strpos($address, $area['name___'])) !== false) {
                $a = mb_substr($address, $pos + mb_strlen($area['name___']));
            }
            if (isset($area['name__']) && ($pos = mb_strpos($address, $area['name__'])) !== false) {
                $a = mb_substr($address, $pos + mb_strlen($area['name__']));
            }
            if (isset($area['name_']) && ($pos = mb_strpos($address, $area['name_'])) !== false) {
                $a = mb_substr($address, $pos + mb_strlen($area['name_']));
            }
            if (isset($area['name']) && ($pos = mb_strpos($address, $area['name'])) !== false) {
                $a = mb_substr($address, $pos + mb_strlen($area['name']));
            }

            $info['address'] = $a;
        } else {

            if ($city['code'] > 0) {
                // var_dump($city);exit;
                // 默认给第一个区
                $info['area'] = [
                    'name' => $city['children'][0]['name'],
                    'code' => $city['children'][0]['code'],
                ];

                $a = '';
                if (isset($city['name__']) && ($pos = mb_strpos($address, $city['name__'])) !== false) {
                    $a = mb_substr($address, $pos + mb_strlen($city['name__']));
                }
                if (isset($city['name_']) && ($pos = mb_strpos($address, $city['name_'])) !== false) {
                    $a = mb_substr($address, $pos + mb_strlen($city['name_']));
                }
                if (isset($city['name']) && ($pos = mb_strpos($address, $city['name'])) !== false) {
                    $a = mb_substr($address, $pos + mb_strlen($city['name']));
                }

                $info['address'] = $a;
            }

        }


        // var_dump($info);exit;

        return $info;
    }
}